from Inference_engine.L1_engine import *
from Inference_engine.L2_engine import *
from Inference_engine.Results_Searcher import *
import wx

'''
主类(入口)
1.西太平洋有什么鱼 or 哪些鱼类分布在西太平洋
2.红点石斑鱼的特征 or 红点石斑鱼简介 or 简介红点石斑鱼 or 简介欧洲䲗 
3.红点石斑鱼分布于哪里 or 红点石斑鱼在哪里有分布
4.红点石斑鱼和欧洲䲗分布在哪里
5.红点石斑鱼属于什么类别?(界门纲目科属种)
......
bio_phylum_cn（门），
    bio_class_cn（纲），bio_order_cn（目），bio_family_cn（科）
'''


class MainFrame(wx.Frame):
    def __init__(self, parent, id, title, size):
        """
        构造器中初始化引擎
        """
        wx.Frame.__init__(self, parent, id, title,
                          style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX |
                                                          wx.MAXIMIZE_BOX))

        self.SetSize(size)
        self.Center()
        self.Tittle = wx.StaticText(self, label='海洋鱼类专家系统', pos=(450, 10), size=(100, 20))
        # self.detectionLabel = wx.StaticText(self, label='Detection Image', pos=(550, 10), size=(40, 25))
        self.inf_Button = wx.Button(self, label='开始推理', pos=(730, 320), size=(150, 80))

        # 绑定单击事件
        self.inf_Button.Bind(wx.EVT_BUTTON, self.main_frame)

        self.questionLabel = wx.StaticText(self, label='请输入问题', pos=(10, 30), size=(100, 20))
        self.questionText = wx.TextCtrl(self, pos=(10, 50), size=(700, 280), style=wx.TE_MULTILINE)
        self.infLabel = wx.StaticText(self, label='推理结果', pos=(10, 350), size=(100, 20))
        self.infText = wx.TextCtrl(self, pos=(10, 380), size=(700, 280),
                                           style=wx.TE_MULTILINE | wx.TE_READONLY)

        self.Show()
        self.L1_engine = L1_engine()
        self.L2_engine = L2_engine()
        self.Results_Searcher = ResultsSearcher()

    def main_frame(self, event):
        # 默认回答
        answer = 'no idea'
        question = self.questionText.GetValue()
        # L1引擎做初步推理
        L1_inf = self.L1_engine.L1_inference(question)
        # 如果列表为空则说明输入的问题不合规或者没有匹配的规则
        if len(L1_inf['args']) == 0:
            self.infText.SetValue(answer)
            return
        # 列表不为空输入到L2引擎做二次推理
        L2_inf = self.L2_engine.L2_inference(L1_inf)
        # 将封装好的cypher语句输入到查询模块
        results = self.Results_Searcher.searcher(L2_inf)
        if len(results) != 0:
            answer = results[0]
        self.infText.SetValue(answer)
        # print(results[0])

    def my_test(self, question):
        """
        测试方法
        :param question:
        :return:
        """
        # 默认回答
        answer = 'no idea'
        # L1引擎做初步推理
        L1_inf = self.L1_engine.L1_inference(question)
        # 如果列表为空则说明输入的问题不合规或者没有匹配的规则
        if len(L1_inf['args']) == 0:
            print(answer)
            return
        # 列表不为空输入到L2引擎做二次推理
        L2_inf = self.L2_engine.L2_inference(L1_inf)
        # 将封装好的cypher语句输入到查询模块
        results = self.Results_Searcher.searcher(L2_inf)
        print(results[0])


if __name__ == '__main__':
    core = MainFrame()
    while 1:
        # 哪些鱼类分布在西太平洋?
        question = input('咨询:')
        core.my_test(question)
